package com.flea.hclv.controller;

import com.alibaba.fastjson.JSON;
import com.flea.hclv.model.entity.HAddress;
import com.flea.hclv.model.entity.HResourceRelation;
import com.flea.hclv.model.entity.HSpot;
import com.flea.hclv.model.entity.vo.SpotVo;
import com.flea.hclv.service.HResourceRelationService;
import com.flea.hclv.service.HSpotService;
import com.flea.hclv.util.ServerResponse;
import com.flea.system.bean.FleaPage;
import com.flea.system.entity.OrgDept;
import com.flea.system.platform.session.FleaOrgUtils;
import com.flea.system.utils.ControllerResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Controller
@RequestMapping("/flea/hclv/spot/")
public class HSpotController {

    @Autowired
    private HSpotService hSpotService;
    @Autowired
    private HResourceRelationService hResourceRelationService;
    Logger logger = LoggerFactory.getLogger(HSpotController.class);


    @RequestMapping("saveOrUpdateSpot")
    public void saveOrUpdateHotel(HttpServletRequest request,
                                  HttpServletResponse response,
                                  @RequestParam("model") String model) {
        HSpot spot = JSON.parseObject(model, HSpot.class);
        HAddress address = JSON.parseObject(model, HAddress.class);
        String userId = FleaOrgUtils.getCurUserUuid(request);
        OrgDept dept = FleaOrgUtils.getCurrentUserDept(userId);
        if (dept == null) {
            ControllerResult cResult = new ControllerResult(false, "部门ID为空,无法修改", false);
            cResult.WriteToPage(response);
            return;
        }
        boolean result = hSpotService.saveOrUpdate(spot, userId, address);
        ControllerResult cResult = new ControllerResult(result, "操作成功", result);
        if (result) {
            String sFiles = request.getParameter("files");
            List<HResourceRelation> files = JSON.parseArray(sFiles, HResourceRelation.class);
            hResourceRelationService.saveImagesRelation(files, userId, spot.getId());
        }
        cResult.WriteToPage(response);
    }

    /**
     * 旅游线路接口a
     *
     * @param request  request
     * @param response response
     */
    @RequestMapping("selectSpotInfo")
    public void selectSpotInfo(HttpServletRequest request, HttpServletResponse response) {
        String userId = FleaOrgUtils.getCurUserUuid(request);
        List<SpotVo> voList = hSpotService.selectSpotInfo(userId);
        ControllerResult conresult = new ControllerResult(voList);
        conresult.set(true, "操作成功");
        conresult.WriteToPage(response);
    }

    /**
     * 查询所有景点接口
     *
     * @param response response
     * @param paging   分页对象
     * @param spotName 景点名称
     * @param level    景点级别
     * @param province 省份
     * @param city     城市
     * @param area     地域
     */
    @RequestMapping("selectAll")
    public void selectAll(HttpServletResponse response, FleaPage<HSpot> paging,
                          @RequestParam(value = "spotName", defaultValue = "") String spotName,
                          @RequestParam(value = "level", defaultValue = "") String level,
                          @RequestParam(value = "province", defaultValue = "") String province,
                          @RequestParam(value = "city", defaultValue = "") String city,
                          @RequestParam(value = "area", defaultValue = "") String area) {
        FleaPage<HSpot> spots = hSpotService.selectAll(paging.getCurPage(), paging.getPageSize(), spotName, province, city, area, level);
        ControllerResult conresult = new ControllerResult(spots);
        conresult.set(true, "操作成功");
        conresult.WritePagingDataToPage(response, spots);
    }

    /**
     * 按当前用户的部门查找景点
     *
     * @param request  request
     * @param paging   分页对象
     * @param spotName 景点名称
     * @param level    景点等级
     * @param province 省份
     * @param city     城市
     * @param area     地区
     * @return list
     */
    @RequestMapping("selectMySpotByDeptId")
    @ResponseBody
    public ServerResponse<FleaPage<HSpot>> selectByDeptId(HttpServletRequest request, FleaPage<HSpot> paging,
                                                          @RequestParam(value = "spotName", defaultValue = "") String spotName,
                                                          @RequestParam(value = "level", defaultValue = "") String level,
                                                          @RequestParam(value = "province", defaultValue = "") String province,
                                                          @RequestParam(value = "city", defaultValue = "") String city,
                                                          @RequestParam(value = "area", defaultValue = "") String area) {
        OrgDept currentUserDept = FleaOrgUtils.getCurrentUserDept(request);
        if (currentUserDept == null) {
            return ServerResponse.createByErrorMessage("没有找到用户的部门信息");
        }
        return hSpotService.selectMySpotByDeptId(currentUserDept.getDeptUuid(), paging.getCurPage(), paging.getPageSize(),
                spotName, province, city, area, level);
    }

    /**
     * 当前部门查找景点
     *
     * @param request request
     * @return list
     */
    @RequestMapping("selectMySpot")
    @ResponseBody
    public ServerResponse<List<HSpot>> selectByDeptId(HttpServletRequest request, HttpServletResponse response) {
        String deptId = FleaOrgUtils.getCurrentUserDeptUuid(request);
        ServerResponse<List<HSpot>> voList = hSpotService.selectByDeptId(deptId);
        return voList;
    }

    /**
     * 根据线路查景点
     *
     * @param lineId
     * @return list
     */
    @RequestMapping("selectByLineId")
    @ResponseBody
    public ServerResponse<List<HSpot>> selectByLineId(String lineId) {

        return hSpotService.selectByLineId(lineId);
    }

    @RequestMapping("selectAllSpots")
    public void selectAllSpots(HttpServletRequest request, HttpServletResponse response) {
        List<HSpot> list = hSpotService.selectAllSpots();
        ControllerResult cResult = new ControllerResult(true, "操作成功", list);
        cResult.WriteToPage(response);
    }

    @RequestMapping("deleteByIds")
    public void deleteByIds(HttpServletRequest request, HttpServletResponse response, @RequestParam("ids") String ids) {
        String userId = FleaOrgUtils.getCurUserUuid(request);
        List<String> idList = JSON.parseArray(ids, String.class);
        if (idList.size() == 0) {
            ControllerResult cResult = new ControllerResult(false, "删除失败");
            cResult.WriteToPage(response);
        } else {
            boolean result = hSpotService.deleteByIds(idList, userId);
            ControllerResult cResult = new ControllerResult(true, "删除成功", result);
            cResult.WriteToPage(response);
        }
    }

}
